﻿/// <remarks>
///系统名称            :   ITALKI 翻译系统
///模块名称            :   Translate
///文件代码            :   
///功能概述            :   
///创建人              :   Snail
///创建日期            :   2008.08.15
///版本号              :   1.0.0.0
///----------< 修改履历 >---------------------------------------------------------------
///修改日期          修改人              修改内容                           版本号
///------------------------------------------------------------------------------------
/// (C) 2007-2008 上海微语信息技术有限公司  版权所有。保留所有权利。
///------------------------------------------------------------------------------------
///</remarks>


namespace Web.Control.WWW.translate
{
    using System;
    using System.Web;
    using BLF.WWW_WWW.translate;
    using Lx.Web.Common.ControlsUI;
    using Model.WWW_WWW.translate;
    using System.Collections;
    using System.Text;
    using System.Data.SqlClient;
    using IProvider.Config;
    using System.Data;
    using System.Xml;
    using Lx.Web.Common.UI;


    /// <summary>
    /// 页面类。
    /// </summary>
    public partial class import : BaseControl
    {
        protected ArrayList LangSets = new ArrayList();
        protected TranslateMember CurrentMember;
        protected string SelLang = "";

        protected override void OnLoad(EventArgs e)
        {
            //判断是否登录
            if (HttpContext.Current.User.Identity.Name == "")
            {
                Response.Redirect(WWWHost + "/User/login.htm?ReturnUrl=" + Request.Url.AbsoluteUri.Split('?')[0]);
            }

            //判断其是否为翻译成员并且是否为管理员
            CurrentMember = Translate.GetTranslateMember(HttpContext.Current.User.Identity.Name);
            if (CurrentMember == null || !CurrentMember.IsAdmin)
                Response.Redirect(WWWHost + "/translate/default.htm");

            if (!string.IsNullOrEmpty(Request.Form["lang"]))
            {
                SelLang = Request.Form["lang"].Trim();
            }

            //读取语言设置XML
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(string.Format(Server.MapPath("LangSet/LangSet_{0}.xml"), Globals.Language));
            }
            catch
            {
                xDoc.Load(string.Format(Server.MapPath("LangSet/LangSet_en-us.xml"), Globals.Language));
            }
            XmlNodeList xnl = xDoc.SelectNodes("//LangSet/Lang");
            if (xnl != null)
            {
                foreach (XmlNode xn in xnl)
                {
                    LangSets.Add(new string[] { xn.ChildNodes[0].InnerText, xn.ChildNodes[1].InnerText });
                }
            }

            //批量添加
            string mutiItem = Request.Form["texta_input"];
            if (!string.IsNullOrEmpty(mutiItem) && mutiItem.Trim()!="")
            {
                mutiItem = mutiItem.Replace("\r", "");
                string[] lines = mutiItem.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

                StringBuilder sb = new StringBuilder();
                foreach (string s in lines)
                {
                    if ((!s.Trim().StartsWith("//"))&&s.IndexOf("=") > 2)
                    {
                        sb.Append(s);
                        sb.Append("\n");
                    }                    
                }
                mutiItem = sb.ToString().Trim('\n');
                if (mutiItem.Length > 2)
                {
                    if (!string.IsNullOrEmpty(Request.Form["chkItem"]))
                    {
                        ImportItem(mutiItem);
                    }
                    else
                    {
                        ImportItem(mutiItem, SelLang, !string.IsNullOrEmpty(Request.Form["IsBest"]));
                    }
                }
            }

            base.OnLoad(e);
        }

        private void ImportItem(string mutiItem, string selLang, bool isBest)
        {
            using (SqlConnection conn = new SqlConnection(WWW_WWW_Translate_Config.DbConnStr))
            {
                string sql = @"Exec ImportItemLang @ItemText,@Lang,@IsBest,@memberno";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add("@ItemText", SqlDbType.NText).Value = mutiItem;
                cmd.Parameters.Add("@Lang", SqlDbType.VarChar).Value = selLang;
                cmd.Parameters.Add("@IsBest", SqlDbType.Bit).Value = isBest;
                cmd.Parameters.Add("@memberno", SqlDbType.VarChar).Value = Page.User.Identity.Name;
                Model.CommonMessage commonMsg = new Model.CommonMessage();
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    Response.Write("<script>alert('operate complete! ')</script>");
                }
                catch (Exception e)
                {
                    Response.Write("<script>alert('" + e.Message.Replace("'","") + "')</script>");
                }
            }
        }

        private void ImportItem(string mutiItem)
        {
            using (SqlConnection conn = new SqlConnection(WWW_WWW_Translate_Config.DbConnStr))
            {
                string sql = "Exec ImportItem @ItemText";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add("@ItemText", SqlDbType.Text).Value = mutiItem;
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    Response.Write("<script>alert('operate complete!')</script>");
                }
                catch (Exception e)
                {
                    Response.Write("<script>alert('" + e.Message.Replace("'", "") + "')</script>");
                }
            }
        }
    }
}